/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.templet.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.templet.dao.EleTempletDao;
import com.thinkgem.jeesite.modules.templet.dao.EleTempletSourceDao;
import com.thinkgem.jeesite.modules.templet.entity.EleTemplet;
import com.thinkgem.jeesite.modules.templet.entity.EleTempletSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 模板管理Service
 * @author ZC
 * @version 2017-04-27
 */
@Service
@Transactional(readOnly = true)
public class EleTempletService extends CrudService<EleTempletDao, EleTemplet> {

	@Autowired
	private EleTempletSourceDao eleTempletSourceDao;
	
	public EleTemplet get(String id) {
		EleTemplet eleTemplet = super.get(id);
		eleTemplet.setEleTempletSourceList(eleTempletSourceDao.findList(new EleTempletSource(eleTemplet)));
		return eleTemplet;
	}
	
	public List<EleTemplet> findList(EleTemplet eleTemplet) {
		return super.findList(eleTemplet);
	}
	
	public Page<EleTemplet> findPage(Page<EleTemplet> page, EleTemplet eleTemplet) {
		return super.findPage(page, eleTemplet);
	}
	
	@Transactional(readOnly = false)
	public void save(EleTemplet eleTemplet, String type) {
        try{
            super.save(eleTemplet);
            for (EleTempletSource eleTempletSource : eleTemplet.getEleTempletSourceList()){
                if (eleTempletSource.getId() == null){
                    continue;
                }
                if (EleTempletSource.DEL_FLAG_NORMAL.equals(eleTempletSource.getDelFlag()) && StringUtils.isNotEmpty(eleTempletSource.getSourceType())){
                    if (StringUtils.isBlank(eleTempletSource.getId())){
                        eleTempletSource.setTemplet(eleTemplet);
                        eleTempletSource.preInsert();
                        eleTempletSourceDao.insert(eleTempletSource);
                    }else{
                        eleTempletSource.preUpdate();
                        if(!"position".equals(type)){
                            eleTempletSource.setXloc(null);
                            eleTempletSource.setYloc(null);
                            eleTempletSource.setWidth(null);
                            eleTempletSource.setHeight(null);
                        }
                        eleTempletSourceDao.update(eleTempletSource);
                    }
                }else{
                    if (StringUtils.isNotEmpty(eleTempletSource.getId())){
                        eleTempletSourceDao.delete(eleTempletSource);
                    }
                }
            }
        }catch(RuntimeException e){logger.error("保存模板失败!");}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleTemplet eleTemplet) {
		super.delete(eleTemplet);
		eleTempletSourceDao.delete(new EleTempletSource(eleTemplet));
	}
}